/* This is a code example of paragraphs listed in Page 135. 
   This program prohibits redirection of input/output no 
   matter how user run this program. /dev/tty always corresponds
   to the terminal device associated with the process that opens
   it. This protects user passwords security.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>

int main()
{
	int  fd ;
	char prompt1[255]  = "Please input your password:";
	char prompt2[255]  = "The password is:";
	char passwd[255]   = "";
	char filePath[255] = "/dev/tty";
	int  passwdLen     = 0;
	int  i;

	fd = open(filePath,O_RDWR);
	write(fd, prompt1, strlen(prompt1)); //the 3rd parameter is needed. If you 
	//leave it out, although comiler no error, but have a mess when run.
	//Following write call also have this case.

	passwdLen = read(fd,passwd);
	write(fd, prompt2, strlen(prompt2));
	write(fd, passwd, passwdLen);

	for(i=0; i < 5; i++)
		write(fd, "H",1);
	write(fd,"\n",1);

	return 0;
}
